#include "OBJ.h"
#include "GLee.h"
#include <SDL.h>
#include <SDL_image.h>
#include <GL/glu.h>

SDL_Surface* screen;

bool initGL(int width, int height)
{
    if(!(screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_OPENGL)))
    {
        return false;
    }

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glViewport(0, 0, (GLsizei)(width), (GLsizei)(height));
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)(height), 1.0f, 10000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
    GLfloat diffuseLight[] = {0.8f, 0.8f, 0.8f, 1.0f};
    GLfloat specularLight[] = {0.5f, 0.5f, 0.5f, 1.0f};
    GLfloat position[] = {0.0f, 0.0f, 0.0f, 1.0f};

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, position);

    return true;
}

OBJ obj;
int motion_x;
int motion_z;
double angle_x;
double angle_z;
double pos_x, pos_z;
bool running;

void controls(double delta)
{
    SDL_Event e;
    while (SDL_PollEvent(&e))
    {
        switch(e.type)
        {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_KEYDOWN:
                switch (e.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        running = false; break;
                    case SDLK_w:
                        motion_z = 1; break;
                    case SDLK_s:
                        motion_z = -1; break;
                    case SDLK_a:
                        motion_x = -1; break;
                    case SDLK_d:
                        motion_x = 1; break;
                    default: break;
                }
                if (e.key.keysym.sym == SDLK_ESCAPE) {
                    running = false;
                }
                
                break;
            case SDL_KEYUP:
                switch (e.key.keysym.sym)
                {
                    case SDLK_w:
                    case SDLK_s:
                        motion_z = 0; break;
                    case SDLK_a:
                    case SDLK_d:
                        motion_x = 0; break;
                    default: break;
                }
                break;
            case SDL_MOUSEMOTION:
                break;
            default: break;
        }
    }
    pos_x += motion_x * delta * 500.0;
    pos_z += motion_z * delta * 500.0;
}

void GLscene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslated(-pos_x, -360.0f, pos_z);
    glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
    glRotatef(-angle_x, 0.0f, 1.0f, 0.0f);
    glRotatef(-angle_z, 0.0f, 0.0f, 1.0f);
    obj.drawTriGL();
    glEnd();
}

int main(int argc, char** argv)
{
    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
    printf("initGL: %s\n", initGL(640, 480) ? "success" : "failure");
   
    //obj.load("../Sterngarten/Sterngarten-Wien_AllesFolgendeReduziertPlatzbedarf_PurgeUnused-FixProblems_beforePurge.obj");
    //obj.load("models/cube.obj");
    obj.load("models/Steinabrunn_Palisades.obj");
    //obj.load("models/palisades_small.obj");

    running = true;

    motion_x = 0;
    motion_z = 0;
    angle_x = 0.0;
    angle_z = 0.0;
    pos_x = 0.0;
    pos_z = -5.0;
    int ticks = SDL_GetTicks();
    while (running)
    {
        int old_ticks = ticks;
        ticks = SDL_GetTicks();
        int diff = ticks - old_ticks;
        controls((double)diff/1000.0);
        GLscene();
        SDL_GL_SwapBuffers();
    }
    SDL_Quit();
    return 0;
}

